/*
 * @Author: wuzhen
 * @Date: 2019-01-22 09:10:12
 * @LastEditors: wuzhen
 * @LastEditTime: 2019-12-07 15:38:14
 * @Description: cookies存储数据
 */
import Cookies from "js-cookie";
import config from "./index.js";

const TokenKey = "token"; // 定义token
const tokenTime = config.cookieExpires; // 设置token过期时间，天为单位

/**
 * @description: 获取token
 * @return:
 */
export function getToken() {
  return Cookies.get(TokenKey);
}

/**
 * @description: 设置token
 * @param {String} token - 用户token
 * @return:
 */
export function setToken(token) {
  // 设置token，并填写有效期
  const maxAge = new Date(
    new Date().getTime() + 24 * 60 * 60 * 1000 * tokenTime
  );
  return Cookies.set(TokenKey, token, {
    expires: maxAge
  });
}

/**
 * @description: 移除token
 * @return:
 */
export function removeToken() {
  return Cookies.remove(TokenKey);
}

/**
 * @description: 设置当前父级路由
 * @param {String} router - 路由名称$route.name
 * @return:
 */
export function setParentrouter(router) {
  return Cookies.set("parentRouter", router);
}

/**
 * @description: 获取当前父级路由
 * @return:
 */
export function getParentrouter() {
  return Cookies.get("parentRouter");
}

/**
 * @description: 移除当前父级路由
 * @return:
 */
export function removeParentrouter() {
  return Cookies.remove("parentRouter");
}

/**
 * @description: 设置语言
 * @return:
 */
export function setLocale(locale) {
  return Cookies.set("locale", locale);
}

/**
 * @description: 获取语言
 * @return:
 */
export function getLocale() {
  return Cookies.get("locale");
}
